package br.inf.solutions.apresentacoes.ssp.control;

import java.util.HashMap;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import br.com.vexillum.util.Return;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.model.Person;
import br.inf.solutions.apresentacoes.ssp.model.interfaces.IExistsPerson;

@Service
@Scope("prototype")
public class SearchPersonController<E extends IExistsPerson> extends
		BaseController<E> {

	public SearchPersonController() {
		super(null);
	}
	
	public SearchPersonController(Class<E> classEntity) {
		super(classEntity);
	}

	public Return getPersonByRG() {
		Return ret = new Return(true);
		Long rg = (Long) data.get("rg");
		
		Person person = getPerson(rg);

		if (person != null) {
			entity.setPerson((Person) person);
		} else {
			ret = new Return(false);
		}
		return ret;
	}

	public Person getPerson(Long rg) {
		HashMap<String, Object> map = new HashMap<>();
		map.put("rg", rg);

		PersonController controller = SpringFactory.getController("personController", PersonController.class, map);
		Person person = controller.searchPersonByRG();
		if(person!=null){
			person = (Person) person.cloneEntity();
		}
		return person;
	}
}